package timing.ukulele.storage.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.storage.service.UserService;
import timing.ukulele.web.controller.BaseController;

/**
 * 常规操作接口
 */
@RestController
@RequestMapping("/common")
@Slf4j
@Tag(name = "通用操作接口")
public class CommonCtroller extends BaseController {
    private final UserService userService;

    public CommonCtroller(UserService userService) {
        this.userService = userService;
    }

    @PutMapping("/password/change")
    @Operation(summary = "重置用户密码")
    public ResponseData<String> change(
            @Parameter(description = "用户名", required = true, example = "1", name = "username", in = ParameterIn.QUERY) @RequestParam("username") String username,
            @Parameter(description = "旧密码", required = true, name = "oldPwd", in = ParameterIn.QUERY) @RequestParam("oldPwd") String oldPwd,
            @Parameter(description = "新密码", required = true, name = "newPwd", in = ParameterIn.QUERY) @RequestParam("newPwd") String newPwd) {
        try {
            return this.userService.change(username, oldPwd, newPwd);
        } catch (IllegalAccessException e) {
            return failResponse("修改密码失败：" + e.getMessage());
        }
    }
}
